package com.yuma.completablefuture;

import java.util.concurrent.*;

public class CompletableFutureDemo3 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        CompletableFuture<Void> cf1 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("第1个编排任务");
        }, threadPool);

        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("第2个编排任务");
        }, threadPool);

        CompletableFuture<Void> cf3 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("第3个编排任务");
        }, threadPool);

        // join 方法，等待所有任务执行完成，再执行后续代码
//        CompletableFuture.allOf(cf1, cf2, cf3).join();

        CompletableFuture.anyOf(cf1, cf2, cf3).join();
        System.out.println("有一个任务肯定完成了");

        threadPool.shutdown();
    }
}
